package com.yunhe.day322.aop;

import com.yunhe.day322.domain.SysLog;
import com.yunhe.day322.service.SysLogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;

@Aspect
@Component
public class SyslogAop {
    @Autowired
    private SysLogService sysService;

    @Autowired
    private HttpServletRequest request;

    private Date startDate;

    //切入点表达式（定义通知追加的位置）
    @Pointcut("execution(* com.yunhe.day322.controller.*.*(..))")
    private void pointCut(){};


    @Before("pointCut()")
    public void before(){

        //功能访问的开始时间
        startDate = new Date();

    }

    @After("pointCut()")
    public void after(JoinPoint joinPoint) {

        //获取功能执行完的时间
        Date endDate = new Date();

        //计算使用时间
        long excTime = endDate.getTime() - startDate.getTime();

        //获取执行方法的所属类的反射对象
        Class cls = joinPoint.getTarget().getClass();

        //获取方法对象
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();


        //获取类之上的RequestMapping注解
        RequestMapping clsa = (RequestMapping) cls.getAnnotation(RequestMapping.class);
        String reqPath = "";
        if (clsa != null) {

            String[] value = clsa.value();
            reqPath = value[0];
        }

        //获取方法之上的RequestMapping注解
        RequestMapping ma = (RequestMapping) method.getAnnotation(RequestMapping.class);
        String[] value1 = ma.value();
        String actionName = value1[0];

        //形成url
        String url = reqPath + "/" + actionName;

        //获取方法名
        String methodName = method.getName();


        //获取ip
        String ip = request.getRemoteAddr();
        ip = ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;

        //获取当前登录用户
        SecurityContext context = SecurityContextHolder.getContext();
        User principal = (User) context.getAuthentication().getPrincipal();
        String username = principal.getUsername();

        //封装为一个syslog对象
        SysLog sysLog = new SysLog();
        sysLog.setIp(ip);
        sysLog.setUrl(url);
        sysLog.setMethod(methodName);
        sysLog.setVisitTime(startDate);
        sysLog.setExecutionTime(excTime);
        sysLog.setUsername(username);

        //调用业务方法
        sysService.addLog(sysLog);

    }
}
